package demo.main;

import static net.sf.dynamicreports.report.builder.DynamicReports.cht;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.report.builder.chart.GanttChartBuilder;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

public class GanttChart extends MutiReport {
	@At("/getGanttChart")
	@Ok("json")
	public String getGanttChart(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("getGanttChart");
		build(request, response);
		return resultResult;
	}

	@Override
	protected void build(HttpServletRequest request, HttpServletResponse response) {
		TextColumnBuilder<String> taskColumn = col.column("任务", "task", type.stringType());
		TextColumnBuilder<Date> actualStartDateColumn = col.column("实际开始时间", "actualstartdate", type.dateType());
		TextColumnBuilder<Date> actualEndDateColumn = col.column("实际结束时间", "actualenddate", type.dateType());
		TextColumnBuilder<Date> scheduledStartDateColumn = col.column("计划开始时间", "scheduledstartdate", type.dateType());
		TextColumnBuilder<Date> scheduledEndDateColumn = col.column("计划结束时间", "scheduledenddate", type.dateType());
		TextColumnBuilder<Double> actualColumn = col.column("实际完成", "actual", type.doubleType());

		GanttChartBuilder chart1 = cht.ganttChart()
			.setTask(taskColumn)
			.series(
				cht.ganttSerie()
					.setStartDate(scheduledStartDateColumn)
					.setEndDate(scheduledEndDateColumn)
					.setLabel("计划进度"),
				cht.ganttSerie()
					.setStartDate(actualStartDateColumn)
					.setEndDate(actualEndDateColumn)
					.setLabel("实际进度"))
			.setTimeAxisFormat(
				cht.axisFormat().setLabel("时间"))
			.setTaskAxisFormat(
				cht.axisFormat().setLabel("任务"));

		GanttChartBuilder chart2 = cht.ganttChart()
			.setTask(taskColumn)
			.series(
				cht.ganttSerie()
					.setStartDate(scheduledStartDateColumn)
					.setEndDate(scheduledEndDateColumn)
					.setPercent(actualColumn)
					.setLabel("计划进度"))
			.setTimeAxisFormat(
				cht.axisFormat().setLabel("时间"))
			.setTaskAxisFormat(
				cht.axisFormat().setLabel("任务"));

		try {
			reportBuilder = report()
				.setTemplate(Templates.reportTemplate)
				.columns(taskColumn, actualStartDateColumn, actualEndDateColumn, scheduledStartDateColumn, scheduledEndDateColumn, actualColumn)
				.title(Templates.createTitleComponent("甘特图"))
				.summary(chart1, chart2)
				.pageFooter(Templates.footerComponent)
				.setDataSource(createDataSource());
			setResult(reportBuilder.toJasperPrint(), request, response);
		} catch (DRException e) {
			e.printStackTrace();
		}
	}

	private JRDataSource createDataSource() {
		DataSource dataSource = new DataSource("task", "actualstartdate", "actualenddate", "scheduledstartdate", "scheduledenddate", "actual");
		dataSource.add("提案", toDate(2011, 1, 5), toDate(2011, 1, 7), toDate(2011, 1, 4), toDate(2011, 1, 7), 1d);
		dataSource.add("分析", toDate(2011, 1, 7), toDate(2011, 1, 14), toDate(2011, 1, 7), toDate(2011, 1, 12), 0.8d);
		dataSource.add("实现", toDate(2011, 1, 15), toDate(2011, 1, 27), toDate(2011, 1, 12), toDate(2011, 1, 25), 0.3d);
		dataSource.add("测试", toDate(2011, 1, 27), toDate(2011, 1, 30), toDate(2011, 1, 25), toDate(2011, 1, 28), 0d);
		return dataSource;
	}

	private Date toDate(int year, int month, int day) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, day);
		return c.getTime();
	}
}
